<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\controller\pay\Julaicai;
use app\common\library\FireFace;
use app\index\library\douyin\MiniApp;
use fast\Http;
use think\Log;
use think\Hook;

/**
 * 示例接口
 */
class Demo extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['test', 'test1'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2'];

    /**
     * 测试方法
     *
     * @ApiTitle    (测试名称)
     * @ApiSummary  (测试描述信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="用户名")
     * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
         'code':'1',
         'msg':'返回成功'
        })
     */
    public function test()
    {
        $content = db('ad_tf_result')->where('id',input('id'))->value('content');
        var_dump($content);
//        $this->fahuo(11913,22516);
//        $this->hxq(9664,0);
//        $this->success('返回成功', $result);
    }

    public function hxq($order_ids,$user_id)
    {
        // 查找对应订单
        $goods = db('wanlshop_order_goods')->where('order_id','in', $order_ids)->column('order_id','goods_id');
        // 查找对应商品id
        $hxq = db('wanlshop_hxq')->where('goods_id','in', array_keys($goods))->select();
        $time = time();
        if ($hxq){
            foreach ($hxq as $item){
                $data = [];
                for ($i=0; $i < $item['issued']; $i++){
                    $data[] = [
                        'order_id' => $goods[$item['goods_id']]??'',
                        'goods_id' => $item['goods_id'],
                        'hxq_id' => $item['id'],
                        'user_id' => $user_id,
                        'shop_id' => $item['shop_id'],
                        'money' => $item['money'],
                        'expire_time' => strtotime("+ {$item['expire_time']} day"),
                        'state'=> 'normal',
                        'createtime'=> $time,
                        'updatetime'=> $time,
                    ];
                }
            }

            db('wanlshop_hxq_user')->insertAll($data);
            return true;
        }else{
            return false;
        }
    }

    public function fahuo($order_ids,$user_id)
    {
        // 订单查找支付信息
        $trade = db('wanlshop_pay_out_trade')->where('order_id', 'in', $order_ids)->whereNotNull('transaction_id')->select();
        $openid = db('wanlshop_third')->where('user_id', $user_id)->value('openid');
        $time = date('c');
        if ($trade) {
            foreach ($trade as $item) {
                $appid = 'wx4b0608b9f9bba7ac';
                $secret = 'c565e6482c4a8b004f056ea4e21e15de';
                $transaction_id = $item['transaction_id'];
                $order_goods =  db('wanlshop_order_goods')->where('order_id', $item['order_id'])->select();
                $shipping_list = [];
                foreach ($order_goods as $goods) {
                    $shipping_list[] = ['item_desc' => $goods['title'] . '*' .$goods['number']];
                }
                $access_token = \app\index\library\wechat\MiniApp::getOtherAccessToken($appid, $secret, 'acctocken' . $appid);
                $url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=$access_token";
                $data = '{
                    "order_key": {
                        "order_number_type": 2,
                        "transaction_id": "' . $transaction_id . '"
                    },
                    "delivery_mode": 1,
                    "logistics_type": 3,
                    "shipping_list": '.json_encode($shipping_list,JSON_UNESCAPED_UNICODE).',
                    "upload_time": "'.$time.'",
                    "payer": {
                        "openid": "' . $openid . '"
                    }
                }';

                $result = Http::post($url, $data, []);
                var_dump($result);
            }
        }
    }
    /**
     * 无需登录的接口
     *
     */
    public function test1()
    {
        $ip = $this->request->ip();
        $this->success('返回成功', $ip);
    }

    /**
     * 需要登录的接口
     *
     */
    public function test2()
    {
        $this->success('返回成功', ['action' => 'test2']);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }

}
